package com.zl.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zl.common.thread.TokenThread;
import com.zl.entity.Collect;
import com.zl.entity.NewsEntity;
import com.zl.entity.Province;
import com.zl.entity.QueryEntity;
import com.zl.service.CollectService;
import com.zl.service.NewsService;
import com.zl.service.SelectorService;
import com.zl.util.JsonMapper;
import com.zl.util.SearchUtils;
import com.zl.weixin.entity.AccessToken;
import com.zl.weixin.entity.UserClient;
import com.zl.weixin.util.WeixinUtil;

/**
 * 首页控制器
 * @author chxl
 *	
 */
@Controller
@RequestMapping("index")
@Scope("prototype")
public class IndexController {
	
	@Autowired
	private SelectorService selectorService;
	@Autowired
	private CollectService collectService;
	/**
	 * 测试
	 * @param model
	 * @return
	 */
	@RequestMapping("test")
	public String test(Model model){
		model.addAttribute("user", "sss");
		return "test";
	}
	
	/**
	 * 首页页面跳转
	 * @return
	 */
	@RequestMapping("index")
	public String index(Model model,HttpServletRequest request){
		String code = request.getParameter("code");//通过微信回调得到的参数
		String appId = "wxac9a290c5dc49ed0";
    	// 第三方用户唯一凭证密钥
    	String appSecret = "24dd8e15cfeb82fc1c0ac60c8705d75c";
    	UserClient userclient = new UserClient();
    	if(StringUtils.isNotBlank(code)){
    		String openId = WeixinUtil.getOpenId(appId, appSecret, code).getOpenid();//通过code调取openid
    		AccessToken at = TokenThread.accessToken;
        	if(at!=null){
        		if(request.getSession().getAttribute("user")==null){
        			if(StringUtils.isBlank(openId)){
        				return "";
        			}
        			userclient= WeixinUtil.getUser(at.getToken(), openId);
	        		request.getSession().setAttribute("user", userclient); //将对象放入session中
	    		}else{
	    			userclient = (UserClient)request.getSession().getAttribute("user");
	    		}//将对象放入session中
        		
        	}else{
        		TokenThread.accessToken = WeixinUtil.getAccessToken(appId, appSecret); 
        		at = TokenThread.accessToken;
        		if(request.getSession().getAttribute("user")==null){
        			if(StringUtils.isBlank(openId)){
        				return "";
        			}
        			userclient= WeixinUtil.getUser(at.getToken(), openId);
	        		request.getSession().setAttribute("user", userclient); //将对象放入session中
	    		}else{
	    			userclient = (UserClient)request.getSession().getAttribute("user");
	    		}
        	}
    	}else{
			userclient = (UserClient)request.getSession().getAttribute("user");
    	}
    	
		List<Province> provinceList = selectorService.getAllProvince();
		model.addAttribute("provinceList", provinceList);
		QueryEntity queryEntity = new QueryEntity();
		queryEntity.setPage(1);
		List<NewsEntity> currentList = SearchUtils.search(queryEntity);
		model.addAttribute("currentList",currentList);
		return "index";
	}
	
	/**
	 *  热门推荐
	 * @return
	 */
	@RequestMapping("recommend")
	@ResponseBody
	public String recommend(String area,HttpServletRequest request){
		UserClient user = (UserClient)request.getSession().getAttribute("user");
//		UserClient user = new UserClient();
//		user.setOpenid("oFOgks6AbGnBns_u2teOtiMG711c");
		QueryEntity queryEntity = new QueryEntity();
		queryEntity.setArea(area);
		queryEntity.setPage(1);
		List<NewsEntity> list = SearchUtils.search(queryEntity);
		for(NewsEntity newsEntity:list){
			List<Collect> collectList = collectService.isExist(user.getOpenid(), newsEntity.getId());
			if(collectList!=null&&collectList.size()>0){
				newsEntity.setCollections("1");
			}else{
				newsEntity.setCollections("0");
			}
		}
		String str = JsonMapper.nonDefaultMapper().toJson(list);
		return str;
	}
	
	
	
}
